/**
 * @file  数据中转站
 * @author  hj
 */
// 引入数据请求API
import {APIS} from 'configPort';

var nativeApi = require('nativeApi');
import Router from 'vue-router';

/* 加载页面 */
import Vue from 'vue';
import we from './vue/we.vue';

Vue.use(Router);

var router = new Router();
router.map({
  '/': {
    component: we
  }
});

export default {
  state: {
    bannerData: [],
    dictionary: {},
    isNewCount: {},
    loginData: {
      username: 'admin',
      password: '111111'
    },
    icons: [],
    menuList: [],
    show: true,
    count: 0,
    total: 0,
    // 订单状态
    orderStatus: -1,
    // 订单列表
    orderList: [],
    // 订单分页
    orderPager: {
      pageNo: 1,
      pageSize: 10,
      totalpages: 0
    },
    orderDetail: {},
    orderCount: {},
    shopRecords: [],
    memberInfo: {},
    listId: null,
    addressList: [],
    pageNo: 1,
    pageSize: 10,
    address: {},
    scrolls: {
      scroll: {}
    },
    isAddress: false
  },
  login: function () {
    var self = this;
    nativeApi.initAjax({
      'type': 'get',
      'url': APIS.login,
      'param': self.state.loginData,
      callback: function (result) {
        if (result && result.code === 200) {
          localStorage.setItem('login_info_test', JSON.stringify(result.data));
          router.go('/staffMenu');
        } else {
          mui.alert('登录失败' || result.msg, '提示');
        }
      }
    });
  },
  // logout: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     'type': 'get',
  //     'url': APIS.logout,
  //     'param': '',
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         localStorage.removeItem('login_info');
  //         self.state.show = true;
  //       }
  //     }
  //   });
  // },
  // getcurrentuserinfo: function () {
  //   nativeApi.initAjax({
  //     'type': 'get',
  //     'url': APIS.getcurrentuserinfo,
  //     'param': '',
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         localStorage.setItem('login_info_test', JSON.stringify(result.data));
  //       } else {
  //         mui.alert('请登录');
  //       }
  //     }
  //   });
  // },
  getBanner: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Banner,
      param: {
        'bannerType': 'carousel_image'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.bannerData = result.data;
        }
      }
    });
  },
  getIcons: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Banner,
      param: {
        'bannerType': 'index_icon_image'
      },
      callback: function (result) {
        console.log(result);
        if (result && result.code === 200) {
          self.state.icons = result.data;
        }
      }
    });
  },
  getDictionary: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Dictionary,
      param: {
        'dicKey': 'hot_recommend'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.dictionary = result.data;
        }
      }
    });
  },
  getIsNewCount: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.IsNewCount,
      callback: function (result) {
        console.log(result);
        if (result && result.code === 200) {
          self.state.count = result.data.count;
        }
      }
    });
  },
  getOrdersCount: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Orders,
      callback: function (result) {
        console.log(result);
        if (result && result.code === 200) {
          self.state.orderCount = result.data;
        }
      }
    });
  },
  getCarts: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Carts,
      data: {
        'cartsType': 'carousel_image'
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.total = result.data.total;
        }
      }
    });
  },
  getOrderList: function (orderStatus, pageSize, pageNo) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.OrderList,
      param: {
        'orderStatus': orderStatus,
        'pageSize': pageSize,
        'pageNo': pageNo
      },
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.orderList = self.state.orderList.concat(result.data);
          self.state.orderPager.totalpages = Math.ceil(result.totalSize / self.state.orderPager.pageSize);
          self.state.orderPager.pageNo++;
          mui('#refreshContainer').pullRefresh().refresh(true);
          mui('#refreshContainer').pullRefresh().endPullupToRefresh((self.state.orderPager.pageNo > self.state.orderPager.totalpages));
          if (result.totalSize === 0) {
            mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
          }
        }
      }
    });
  },
  getOrderDetail: function (id) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.OrderList + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.orderDetail = result.data;
        }
      }
    });
  },
  // 上拉
  getShopRecords: function (date) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Records,
      param: {
        'pageSize': self.state.pageSize,
        'pageNo': self.state.pageNo,
        date: date
      },
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          if (result.data.records.length > 0) {
            self.state.shopRecords = self.state.shopRecords.concat(result.data.records);
            console.log(self.state.pageNo);
            console.log('werrrr' + result.data.records.length);
            self.state.pageNo = self.state.pageNo + 1;
          }
          if (result.data.records.length < self.state.pageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.refresh(true);
        if (result.totalSize === '') {
          self.state.scrolls.scroll.disablePullupToRefresh();
        }
        self.state.scrolls.scroll.endPullupToRefresh(endPullupToRefresh);
        // self.state.scrolls.scroll.refresh(true);
        // mui('#refreshContainer').pullRefresh().refresh(true);
      }
    });
  },
  // 下拉
  getShopRecordsUP: function (date) {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Records,
      param: {
        'pageSize': self.state.pageSize,
        'pageNo': self.state.pageNo,
        date: date
      },
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.shopRecords = self.state.shopRecords.concat(result.data.records);
          self.state.shopRecords.pointTotal = result.data.pointTotal;
          if (result.data.records.length > 0) {
            self.state.pageNo = self.state.pageNo + 1;
          }
          if (result.data.records.length < self.state.pageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.refresh(true);
        if (result.totalSize === '') {
          self.state.scrolls.scroll.disablePullupToRefresh();
        }
        self.state.scrolls.scroll.endPullupToRefresh(endPullupToRefresh);
      }
    });
  },
  getMemberInfo: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.memberInfo,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.memberInfo = result.data;
        }
      }
    });
  },
  confirmGet: function (listId) {
    var self = this;
    nativeApi.initAjax({
      type: 'put',
      url: APIS.OrderList + '/' + listId,
      callback: function (result) {
        if (result && result.code === 200) {
          // 删除列表中已收货的
          self.state.orderList = [];
          self.state.orderPager.pageNo = 1;
          self.getOrderList(2, self.state.orderPager.pageSize, self.state.orderPager.pageNo);
          /* self.state.orderList = self.state.orderList.filter(
            function (v, i, arr) {
              v.id !== listId;
            }
          );*/
        }
      }
    });
  },
  /* 翻页相关*/
  setScroll: function (scroll) {
    this.state.scrolls.scroll = scroll;
  },
  getAddressList: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.getAddressId,
      param: {
        'pageSize': self.state.pageSize,
        'pageNo': self.state.pageNo
      },
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.addressList = self.state.addressList.concat(result.data);
          if (result.data.length > 0) {
            self.state.pageNo = self.state.pageNo + 1;
          }
          if (result.data.length < self.state.pageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        if (result.totalSize === '') {
          self.state.scrolls.scroll.disablePullupToRefresh();
        }
        self.state.scrolls.scroll.endPullupToRefresh(endPullupToRefresh);
      }
    });
  },
  getAddressListUP: function () {
    var self = this;
    nativeApi.initAjax({
      type: 'get',
      url: APIS.getAddressId,
      param: {
        'pageSize': self.state.pageSize,
        'pageNo': self.state.pageNo
      },
      callback: function (result) {
        var endPullupToRefresh = false;
        if (result && result.code === 200) {
          self.state.addressList = result.data.concat(self.state.addressList);
          if (result.data.length > 0) {
            self.state.pageNo = self.state.pageNo + 1;
          }
          if (result.data.length < self.state.pageSize) {
            endPullupToRefresh = true;
          }
        } else {
          endPullupToRefresh = true;
        }
        self.state.scrolls.scroll.endPulldownToRefresh(endPullupToRefresh);
      }
    });
  },
  getAddressId: function (id) {
    var self = this;
    nativeApi.initAjax({
      type: 'GET',
      url: APIS.getAddressId + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.address = result.data;
        }
      }
    });
  },
  saveAddress: function (id) {
    var self = this;
    if (self.state.address.province === '' || !self.state.address.province) {
      mui.alert('地区错误');
      return false;
    }
    if (self.state.address.receiverName.trim() === '') {
      mui.alert('收货人不能为空');
      return false;
    }
    if (self.state.address.receiverPhone.trim() === '') {
      mui.alert('联系电话不能为空');
      return false;
    }
    if (self.state.address.detailAddress.trim() === '') {
      mui.alert('详细地址不能为空');
      return false;
    }
    if (!/^[1][34578][0-9]{9}$/.test(self.state.address.receiverPhone)) {
      mui.alert('手机号码错误');
      return false;
    }
    nativeApi.initAjax({
      type: 'POST',
      url: APIS.getAddressId + '/' + id,
      param: self.state.address,
      callback: function (result) {
        if (result && result.code === 200) {
          mui.alert('修改成功', function () {
            window.history.back(-1);
          });
        } else {
          mui.alert(result.msg);
        }
      }
    });
  },
  setAddress: function (province, city, region) {
    var self = this;
    self.state.address.province = province;
    self.state.address.city = city;
    self.state.address.region = region;
  },
  setNotDefult: function (index) {
    var self = this;
    var id = self.state.addressList[index].id;
    nativeApi.initAjax({
      type: 'PUT',
      url: APIS.addressNotDefult + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.addressList[index]['isDefult'] = 0;
        } else {
          self.state.addressList[index]['isDefult'] = 1;
          mui.alert(result.msg);
        }
      }
    });
  },
  setDefult: function (index) {
    var self = this;
    var id = self.state.addressList[index].id;
    nativeApi.initAjax({
      type: 'PUT',
      url: APIS.getAddressId + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          var list = JSON.parse(JSON.stringify(self.state.addressList));
          for (let i = 0; i < list.length; i++) {
            list[i]['isDefult'] = 0;
          }
          list[index]['isDefult'] = 1;
          self.state.addressList = list;
        } else {
          mui.alert(result.msg);
        }
      }
    });
  },
  deleAddress: function (index) {
    var self = this;
    var id = self.state.addressList[index].id;
    nativeApi.initAjax({
      type: 'DELETE',
      url: APIS.getAddressId + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          self.state.addressList.splice(index, 1);
          // this.state.scrolls.scroll
          mui.alert('删除成功');
        } else {
          mui.alert(result.msg);
        }
      }
    });
  },
  addAddress: function (param) {
    nativeApi.initAjax({
      type: 'POST',
      url: APIS.getAddressId,
      param: param,
      callback: function (result) {
        if (result && result.code === 200) {
          mui.alert('添加成功', function () {
            window.history.back(-1);
          });
        } else {
          mui.alert(result.msg);
        }
      }
    });
  },
  // 查看商品详情
  lookGoodsDetail: function (id) {
    nativeApi.initAjax({
      type: 'get',
      url: APIS.Goods + '/' + id,
      callback: function (result) {
        if (result && result.code === 200) {
          window.location.href = '../../mallHome/index.html#/goods/' + id;
        } else {
          mui.alert(result.msg, function () {
          });
        }
      }
    });
  }
  // getAllOrderList: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     type: 'get',
  //     url: APIS.OrderList,
  //     param: {
  //       'orderStatus': -1,
  //       'pageSize': 10,
  //       'pageNo': 1
  //     },
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         self.state.all = result.data;
  //       }
  //     }
  //   });
  // },
  // getNotSentOrderList: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     type: 'get',
  //     url: APIS.OrderList,
  //     param: {
  //       'orderStatus': 0,
  //       'pageSize': 10,
  //       'pageNo': 1
  //     },
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         self.state.notsent = result.data;
  //       }
  //     }
  //   });
  // },
  // getSentOrderList: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     type: 'get',
  //     url: APIS.OrderList,
  //     param: {
  //       'orderStatus': 1,
  //       'pageSize': 10,
  //       'pageNo': 1
  //     },
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         self.state.sent = result.data;
  //       }
  //     }
  //   });
  // },
  // getCompletedOrderList: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     type: 'get',
  //     url: APIS.OrderList,
  //     param: {
  //       'orderStatus': 2,
  //       'pageSize': 10,
  //       'pageNo': 1
  //     },
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         self.state.completed = result.data;
  //       }
  //     }
  //   });
  // },
  // getCancelOrderList: function () {
  //   var self = this;
  //   nativeApi.initAjax({
  //     type: 'get',
  //     url: APIS.OrderList,
  //     param: {
  //       'orderStatus': 3,
  //       'pageSize': 10,
  //       'pageNo': 1
  //     },
  //     callback: function (result) {
  //       if (result && result.code === 200) {
  //         self.state.cancel = result.data;
  //       }
  //     }
  //   });
  // }
};
